<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>登录页面</title>
<link rel="icon" href="favicon.ico" type="image/ico">
<meta name="keywords" content="LightYear,光年,后台模板,后台管理系统,光年HTML模板">
<meta name="description" content="LightYear是一个基于Bootstrap v3.3.7的后台管理系统的HTML模板。">
<meta name="author" content="yinqi">
<link href="/lib/css/bootstrap.min.css" rel="stylesheet">
<link href="/lib/css/materialdesignicons.min.css" rel="stylesheet">
<link href="/lib/css/style.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/lib/layui/css/layui.css">
<style>
.lyear-wrapper {
    position: relative;
}
.lyear-login {
    display: flex !important;
    min-height: 100vh;
    align-items: center !important;
    justify-content: center !important;
}
.lyear-login:after{
    content: '';
    min-height: inherit;
    font-size: 0;
}
.login-center {
    background: #fff;
    min-width: 29.25rem;
    padding: 2.14286em 3.57143em;
    border-radius: 5px;
    margin: 2.85714em;
}
.login-header {
    margin-bottom: 1.5rem !important;
}
.login-center .has-feedback.feedback-left .form-control {
    padding-left: 38px;
    padding-right: 12px;
}
.login-center .has-feedback.feedback-left .form-control-feedback {
    left: 0;
    right: auto;
    width: 38px;
    height: 38px;
    line-height: 38px;
    z-index: 4;
    color: #dcdcdc;
}
.login-center .has-feedback.feedback-left.row .form-control-feedback {
    left: 15px;
}
</style>
</head>
  
<body>
<div class="row lyear-wrapper">
  <div class="lyear-login">
    <div class="login-center">
      <div class="login-header text-center">
        <a href="index.html" style="color: #00FF00;font-size:24px"> 农业信息化后台管理系统</a>
      </div>
      <form action="#!" method="post">
        <div class="form-group has-feedback feedback-left">
          <input type="text" placeholder="请输入您的用户名" class="form-control" name="username" id="username" />
          <span class="mdi mdi-account form-control-feedback" aria-hidden="true"></span>
        </div>
        <div class="form-group has-feedback feedback-left">
          <input type="password" placeholder="请输入密码" class="form-control" id="password" name="password" />
          <span class="mdi mdi-lock form-control-feedback" aria-hidden="true"></span>
        </div>
        <div class="form-group has-feedback feedback-left row">
          <div class="col-xs-7">
            <input type="text" name="captcha" id="yzcode" class="form-control" placeholder="验证码">
            <span class="mdi mdi-check-all form-control-feedback" aria-hidden="false"></span>
          </div>
          <div class="col-xs-5">
            <canvas class="pull-right" id="canvas" style="cursor: pointer;" onclick="" title="点击刷新" alt="captcha" width="100" height="30"></canvas>
          </div>
        </div>
        <div class="form-group">
          <button class="btn btn-block btn-primary" type="button" id="login">立即登录</button>
          <br><div>没有账号？<a style="color: #00F7DE;" href="/register">点击注册</a></div>
        </div>
      </form>
    </div>
  </div>
</div>
<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script>
<script>
  $(function(){
    var show_num = [];
    draw(show_num);
    $("#canvas").on('click',function(){
      draw(show_num);
    })
    $("#login").on('click',function(){
      var val = $("#yzcode").val().toLowerCase();
      var num = show_num.join("");
      if(val==''){
        alert('请输入验证码！');
      }else if(val == num){
          var username=$("#username").val()
          var password=$("#password").val()
        if(username==""){
            alert('请输入用户名！');
        }else if (password==""){
            alert('请输入密码！')
        }else {
            $.ajax({
                url:'/login',
                type:'post',
                data:{
                    username:username,
                    password:password
                },
                success:function (res) {
                    if (res.code==0){
                      location.href = "/index";
                    }else {
                        alert('用户名或密码错误！')
                    }
                }
            })
        }
        $(".input-val").val('');
        // draw(show_num);
      }else{
        alert('验证码错误！请重新输入！');
        $(".input-val").val('');
        // draw(show_num);
      }
    })
  })
  //生成并渲染出验证码图形
  function draw(show_num) {
    var canvas_width=$('#canvas').width();
    var canvas_height=$('#canvas').height();
    var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
    var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
    canvas.width = canvas_width;
    canvas.height = canvas_height;
    var sCode = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
    var aCode = sCode.split(",");
    var aLength = aCode.length;//获取到数组的长度
    for (var i = 0; i < 4; i++) { //这里的for循环可以控制验证码位数（如果想显示6位数，4改成6即可）
      var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
      // var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
      var deg = Math.random() - 0.5; //产生一个随机弧度
      var txt = aCode[j];//得到随机的一个内容
      show_num[i] = txt.toLowerCase();
      var x = 10 + i * 20;//文字在canvas上的x坐标
      var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
      context.font = "bold 23px 微软雅黑";
      context.translate(x, y);
      context.rotate(deg);
      context.fillStyle = randomColor();
      context.fillText(txt, 0, 0);
      context.rotate(-deg);
      context.translate(-x, -y);
    }
    for (var i = 0; i <= 5; i++) { //验证码上显示线条
      context.strokeStyle = randomColor();
      context.beginPath();
      context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
      context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
      context.stroke();
    }
    for (var i = 0; i <= 30; i++) { //验证码上显示小点
      context.strokeStyle = randomColor();
      context.beginPath();
      var x = Math.random() * canvas_width;
      var y = Math.random() * canvas_height;
      context.moveTo(x, y);
      context.lineTo(x + 1, y + 1);
      context.stroke();
    }
  }
  //得到随机的颜色值
  function randomColor() {
    var r = Math.floor(Math.random() * 256);
    var g = Math.floor(Math.random() * 256);
    var b = Math.floor(Math.random() * 256);
    return "rgb(" + r + "," + g + "," + b + ")";
  }
</script>
<script src="/lib/layui/layui.js"></script>
<script type="text/javascript" src="/lib/js/jquery.min.js"></script>
<script type="text/javascript" src="/lib/js/bootstrap.min.js"></script>
<script type="text/javascript">;</script>
</body>
</html>